#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import time
import yagmail
import smtplib
import datetime
#import commands
from holidayutil import Holidayutil
from dateutil import Dateutil
from mailutil import MailSend

import os


def listdeal(str, week):
    print str ,week
    if week == 4:
        temlist = str[0:4]
    else:
        temlist = str[week:4] + str[0:week]

    print temlist,'temlist'
    for i, line in enumerate(temlist):
        #print line[2],'line[2]'
        i + 1
        if i > 4:
            break
        else:
            pingstr = 'ping -n 2 -w 1 ' +line[2]
            print pingstr
            try:
                exit_code = os.system(pingstr)
                #print  exit_code
                if exit_code == 0:
                    print 'ping成功',line[2], line[3]
                    MailSend().send(str(line[3]))
                    break;
                else:
                    print 'ping失败', line[2], line[3]

                    continue
            except Exception, e:
                 print e + '捕获了一个异常'


def start(self):
    list = [[['11', '汪文', '192.168.31.115', '304589855@qq.com'],
             ['12', '张瑶', '192.168.31.223', '376324789@qq.com'],
             ['13', '周逸轩', '192.168.31.215', '1554961698@qq.com'],
             ['14', '郭晨', '192.168.31.109', '517273803@qq.com'],
             ['15', '卢丽鹏', '192.168.31.56', '214377877@qq.com']]
            ]
    '''
               [['21', 'wangtao', '192.168.1.191', '22221111@qq.com1'],
             ['22', 'wangtao2', '192.168.1.191', '2222222@qq.com2'],
             ['23', 'wangtao3', '192.168.1.5', '2222333@qq.com3'],
             ['24', 'wangtao4', '192.168.1.191', '222244444@qq.com4'],
             ['25', 'wangtao5', '192.168.1.191', '22225555@qq.com5']]
    
    '''
    year = time.strftime('%Y', time.localtime(time.time()))
    month = time.strftime('%m', time.localtime(time.time()))
    day = time.strftime('%d', time.localtime(time.time()))
    week = time.strftime('%w', time.localtime(time.time()))
    print week, 'week'
    count = Dateutil().get_week_of_month(int(year), int(month), int(day));

    holiday = year + '' + month + '' + day;
    print holiday;
    result = Holidayutil().get_holiday_result(holiday);

    print  len(list), ' len(list)'
    print  count, ' count'
    if int(count) > len(list):  # 首先判断是第几周

        # result 工作日
        if int(result) == 0 or int(result) == 2:
            # print  list[0][int(week)-1][0]
            # 超过几周 就前置选前面的同事
            tempcount = count%len(list);

            pingstr = 'ping -n 2 -w 1 '+list[tempcount][int(week) - 1][2]
            print str

            try:
                exit_code = os.system(pingstr)
                print  exit_code
                if exit_code == 0:
                    print 'ping成功',list[tempcount][int(week) - 1][2]
                    print  list[tempcount][int(week) - 1][3]
                    MailSend().send(list[tempcount][int(week) - 1][3]);
                else:
                    print 'ping失败'
                    listdeal(list[tempcount], int(week))
            except Exception, e:
                print e + '捕获了一个异常'
        else:
            print  list[0][int(week) - 1][0]
            print  '节假日不需要值班'

    else:
        if result == 0 or result == 2:
            '''
            pingstr = 'ping ' + list[int(count - 1)][int(week) - 1][2]
            print pingstr,'end===pingstr'
            exit_code = os.system(pingstr)
            print exit_code,'===exit_code'
            if exit_code == 0:
                MailSend(list[int(count - 1)][int(week) - 1][2])
            else:
                listdeal(list[int(count - 1)], int(week))
            # MailSend( list[int(count-1)][int(week)-1][2])
            '''
            pingstr = 'ping -n 2 -w 1 ' +list[int(count - 1)][int(week) - 1][2]
            try:
                exit_code = os.system(pingstr)
                #print  exit_code
                if exit_code == 0:
                    print 'ping成功',list[int(count - 1)][int(week) - 1][2],list[int(count - 1)][int(week) - 1][3]
                    MailSend().send(list[int(count - 1)][int(week) - 1][3])
                else:
                    print 'ping失败'
                    listdeal(list[int(count - 1)], int(week))
                    # MailSend( list[int(count-1)][int(week)-1][2])
            except Exception, e:
                print e + '捕获了一个异常'
        else:
            # print  list[int(count-1)][int(week)-1][0]
            print  '节假日不需要值班'


if __name__ == '__main__':
    '''
    str = 'ping '+'192.168.1.191'
    print str
    try:
        exit_code = os.system('ping -n 2 -w 1 192.168.1.5')
        print  exit_code
        if exit_code == 0:
            print 'ping成功'
        else:
            print 'ping失败'
    except Exception, e:
        print e+'捕获了一个异常'
    #exit_code = os.system(str)


    '''
    start('')
